package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.AdminMapper;
import com.qztc.hire_end_core.util.MD5Parse;
import com.qztc.hire_end_dto.dto.Admin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/admin")
@Tag(name = "管理员接口")
public class AdminController {
    private final AdminMapper adminMapper;

    public AdminController(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public long login(@RequestBody Admin admin) {
        admin.setAdminPwd(MD5Parse.encryptMD5(admin.getAdminPwd()));
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", admin.getAdminName());

        queryWrapper.eq("admin_pwd", admin.getAdminPwd());
        return new Admin().selectCount(queryWrapper);
    }

    @Operation(summary = "删除管理员")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody Admin admin) {
        return admin.deleteById();
    }

    @Operation(summary = "添加管理员")
    @PostMapping("/add")
    public boolean add(@RequestBody Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", admin.getAdminName());
        if(admin.selectCount(queryWrapper)>0){
            return false;
        }
        admin.setAdminPwd(MD5Parse.encryptMD5(admin.getAdminPwd()));
        return admin.insert();
    }

    @Operation(summary = "根据adminId查找管理员")
    @PostMapping("/getOneAdmin")
    public Admin getOneAdmin(@RequestParam Integer adminId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        return new Admin().selectOne(queryWrapper);
    }

    @Operation(summary = "根据adminId修改管理员信息")
    @PutMapping("/update")
    public boolean update(@RequestBody Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_name", admin.getAdminName());
        if(admin.selectCount(queryWrapper)>0){
            return false;
        }
        admin.setAdminPwd(MD5Parse.encryptMD5(admin.getAdminPwd()));
        return admin.updateById();
    }

    @Operation(summary = "查询所有管理员")
    @GetMapping("/getAll")
    public List<Admin> get() {
        return new Admin().selectAll();
    }

    @Operation(summary = "批量删除管理员")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> adminIds) {
        if(adminIds == null || adminIds.isEmpty()){
            return false;
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(
                "admin_id", adminIds);
        return new Admin().delete(queryWrapper);
    }

    @Operation(summary = "批量增加管理员")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<Admin> admins) {
        if(admins == null || admins.isEmpty()){
            return false;
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("admin_name",admins.stream().map(Admin::getAdminName).collect(Collectors.toList()));
        if(adminMapper.selectCount(queryWrapper)>0){
            return false;
        }
        String sql = "insert into admin(admin_name,admin_pwd) values"+
                admins.stream().map(Admin -> String.format("('%s','%s')", Admin.getAdminName(), MD5Parse.encryptMD5(Admin.getAdminPwd())))
                        .collect(Collectors.joining(","));
        return adminMapper.executeSql(sql);
    }
}
